package com.hframe.log;

import com.hframe.utils.FrameUtils;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.helpers.LogLog;

import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class InitLog {
    static Logger log = Logger.getLogger(InitLog.class);

    public InitLog() {

    }

    public void initialized(ServletContext context) {
        String propertiesPath = this.getClass().toString();
        int index = propertiesPath.lastIndexOf(".");
        propertiesPath = propertiesPath.substring(index + 1, propertiesPath.length());
        try {
            InputStream in = this.getClass().getResourceAsStream(propertiesPath + ".properties");
            setOutPath(in, context.getRealPath("/WEB-INF/"));
            in.close();
        } catch (Exception e) {
            log.error(e, e);
        }

    }

    public void destroyed(ServletContext context) {

    }

    public void setOutPath(InputStream istream, String filePath) {
        Properties props = new Properties();
        try {
            props.load(istream);
            istream.close();

            String logFile = filePath + props.getProperty("log4j.appender.debug.File");
            createDirs(logFile = logFile.replace('\\', '/'));
            props.setProperty("log4j.appender.debug.File", logFile);

            logFile = filePath + props.getProperty("log4j.appender.info.File");
            createDirs(logFile = logFile.replace('\\', '/'));
            props.setProperty("log4j.appender.info.File", logFile);

            logFile = filePath + props.getProperty("log4j.appender.error.File");
            createDirs(logFile = logFile.replace('\\', '/'));
            props.setProperty("log4j.appender.error.File", logFile);

            logFile = filePath + props.getProperty("log4j.appender.warn.File");
            createDirs(logFile = logFile.replace('\\', '/'));
            props.setProperty("log4j.appender.warn.File", logFile);

            logFile = filePath + props.getProperty("log4j.appender.fatal.File");
            createDirs(logFile = logFile.replace('\\', '/'));
            props.setProperty("log4j.appender.fatal.File", logFile);

            logFile = filePath + props.getProperty("log4j.appender.sql.File");
            createDirs(logFile = logFile.replace('\\', '/'));
            props.setProperty("log4j.appender.sql.File", logFile);

            if (FrameUtils.isRunDebug()) {
                logFile = "all," + props.getProperty("log4j.rootLogger");
                props.setProperty("log4j.rootLogger", logFile);
                LogLog.setInternalDebugging(true);
            }

            PropertyConfigurator.configure(props);// 装入 log4j 配置信息
            log.info("LOG4J 日志-----加载 日志文件位置：" + filePath);

        } catch (IOException ioe) {
            log.error(ioe);
            ioe.printStackTrace();
        }
    }


    private void createDirs(String fliePath) {
        File file = new File(fliePath);
        File dir = new File(file.getParent());
        if (!dir.exists() && !dir.isDirectory()) {
            dir.mkdirs();
        }
    }
}
